MyMineControl = {}
local self = MyMineControl
Control:Register(self)

function self:Init()
    Debug.Log("MyMineControl Init")
    self:InitCell()

   
end

function self:UnInit()

end

function self:InitCell()
    local cell = nil
    local idx = 0
    for y = 1 , 14 do 
        for x = 1 ,9 do 
            idx = x + (y-1) * 9
            cell = MyMineModel:MakeCell(idx, x, y)
            MyMineModel:AddCell(cell)
        end
    end
end
function self:OnStart()
    MyMineModel.game_status=MyMineConstant.GameStatus.Start
    --[[
         bomb_num=0 ,--附件炸弹数
        is_bomb=false,--炸弹
        is_dig=false,--挖
    --]]
    local cell=nil
    for y = 1 , 14 do 
        for x = 1 ,9 do 
            cell = MyMineModel:GetCell(x, y)
            cell.bomb_num=0
            cell.is_bomb=false
            cell.is_dig=false
           
        end
    end
    --清除记录当前炸弹数
    MyMineModel.bomb_count=0
    --随机填充炸弹
    --1.是否已经填满炸弹
    --2.否就一直填炸弹
    while(not MyMineModel:IsBombFull())do
        MyMineModel:AddBomb()
        
    end
    local x=0
    local y=0
    local num=0

    --计算格子附件的炸弹数
    MyMineModel:ForeachCell(function(cell)
        x=cell.x
        y=cell.y
        num=0

        --上
        if(MyMineModel:IsBomb(x-1,y-1)) then
            num=num+1
        end
        if(MyMineModel:IsBomb(x,y-1)) then
            num=num+1
        end
        if(MyMineModel:IsBomb(x+1,y-1)) then
            num=num+1
        end
        --中
        if(MyMineModel:IsBomb(x-1,y)) then
            num=num+1
        end
        if(MyMineModel:IsBomb(x+1,y)) then
            num=num+1
        end
        --下
        if(MyMineModel:IsBomb(x-1,y+1)) then
            num=num+1
        end
        if(MyMineModel:IsBomb(x,y+1)) then
            num=num+1
        end
        if(MyMineModel:IsBomb(x+1,y+1)) then
            num=num+1
        end
        Debug.Log(num)
        cell.bomb_num=num
    end)
EventListener:Broadcast(MyMineEvent.MINE_ALL_UP)
end
function self:OnDig(idx,x,y)
    if MyMineConstant.game_status==MyMineConstant.GameStatus.GameOver then
        return
    end

    local cell=MyMineModel:GetCell(x,y)
    if cell then
        if cell.is_dig then return end
        --空地
        if cell.bomb_num==0 then
            self:DigEmpty(x,y)
        end

        cell.is_dig=true

        --炸弹
        if cell.is_bomb then
            --游戏结束
            MyMineModel.game_status=MyMineConstant.GameStatus.GameOver
       
        end
        EventListener:Broadcast(MyMineEvent.MINE_ALL_UP)
end
end
function self:DigEmpty(x,y)
    local cell=MyMineModel:GetCell(x,y)

    if cell then
        --如果挖到附近有炸弹，就停止不挖
        if cell.is_dig==false and cell.bomb_num ~=0 then
            cell.is_dig=true
            return
        end
        --如果没有被挖，并且附近炸弹为0 并且不是炸弹 继续挖

        if cell.is_dig==false and cell.bomb_num==0 and cell.is_bomb~= true then
            cell.is_dig=true
            self:DigEmpty(x-1,y-1)
            self:DigEmpty(x,y-1)
            self:DigEmpty(x+1,y-1)

            self:DigEmpty(x-1,y)
            self:DigEmpty(x+1,y)

            self:DigEmpty(x-1,y+1)
            self:DigEmpty(x,y+1)
            self:DigEmpty(x+1,y+1)
        end
    end
end
